const app = getApp();
const {
  envList
} = require('../../../envList.js');
let phone = ''
let code_input = ''
let code = ''
let user_name = ''
let address2 = ''
Page({
  data: {
    address1: ['江苏省', '南京市', '玄武区']
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      card_id: options.card_id,
      card_number: options.card_number,
      card_password: options.card_password,
      goods_name: options.goods_name,
      goods_url: options.goods_url,
      goods_id: options.goods_id,
      specs: options.specs,
      type: options.type,
      goods_number: options.goods_number,
      goods_explain: options.goods_explain,
      date: that.getNowDate()
    })
  },
  // 改变地址
  RegionChange: function (e) {
    var that = this;
    that.setData({
      address1: e.detail.value
    })
  },
  // 改变日期
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  //获取当前日期
  getNowDate() {
    var date = new Date();
    var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    return todate;
  },
  //获取收件人名
  getUserName(event) {
    user_name = event.detail.value
  },
  //获取用户输入的手机号
  getPhone(event) {
    phone = event.detail.value
  },
  //获取用户输入的验证码
  getCode(event) {
    code_input = event.detail.value
  },
  //获取详细地址
  getAddress(event) {
    var that = this;
    address2 = event.detail.value,
      that.setData({
        address: that.data.address1[0] + that.data.address1[1] + that.data.address1[2] + address2
      })
  },
  //发送验证码
  send() {
    var that = this;
    if (phone != '' && phone.length == 11) {
      that.setData({
        code_able: 'disabled'
      });
      wx.showToast({
        title: '验证码已发送',
        icon: 'success'
      })
      code = this.generateMixed(4);
      // console.log('点击了发送', phone);
      // console.log('生成验证码', code);
      // 调用云函数
      wx.cloud.callFunction({
        name: 'ToolFunctions',
        data: {
          type: "sendSMS",
          phone: phone,
          code: code,
          envId: envList[0].envId
        }
      }).then(res => {
        console.log('成功返回信息', res)
      }).catch(res => {
        console.log('失败返回信息', res)
      })
    } else {
      wx.showToast({
        title: '手机号码错误',
        icon: 'error'
      })
    }
  },
  //验证提交
  submitInfo() {
    var that = this;
    if (code_input == code) {
      if (user_name == '' || phone == '' || code == '' || address2 == '') {
        wx.showToast({
          title: '信息须填完整',
          icon: 'error',
          duration: 2000
        })
      } else {
        wx.requestSubscribeMessage({
          tmplIds: ['jlF85j8mQfZi6lLtr2zLKfxP7GX0_rOGhSnuiCL8gFU'],
          success(res) {
            console.log(res);
            if (res.errMsg == "requestSubscribeMessage:ok" && res.jlF85j8mQfZi6lLtr2zLKfxP7GX0_rOGhSnuiCL8gFU == 'accept') {
              // 录入提货信息
              that.insertInfo();
            } else {
              wx.showModal({
                title: '订阅失败',
                content: '未订阅全部通知将无法收到订单提醒，请重新授权',
                showCancel: false,
              })
            }
          },
          fail(err) {
            wx.showToast({
              icon: 'error',
              title: '授权失败',
            })
          }
        })
      }
    } else {
      wx.showToast({
        icon: 'error',
        title: '验证码错误',
        duration: 2000
      })
    }
  },
  //生成随机验证码,n代表几位
  generateMixed(n) {
    let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let res = "";
    for (var i = 0; i < n; i++) {
      var id = Math.ceil(Math.random() * 9);
      res += chars[id];
    }
    return res;
  },
  // 录入提货信息
  insertInfo() {
    this.setData({
      btn_disabled: 'disabled'
    });
    wx.showLoading({
      title: '系统正在处理',
    })
    var that = this;
    wx.cloud.callFunction({
      name: "Picking",
      data: {
        card_id: that.data.card_id,
        user_name: user_name,
        user_address: that.data.address,
        user_openid: wx.getStorageSync('openid'),
        user_phone_number: phone,
        goods_id: that.data.goods_id,
        goods_number: that.data.goods_number,
        user_delivery_date: that.data.date,
        status: 1
      },
      success: res => {
        wx.hideLoading();
        // 提货成功
        wx.showModal({
          title: '系统提示',
          content: '提货成功，请等待发货。发货后可通过订单查询物流信息。',
          showCancel: false,
          confirmText: '确认',
          confirmColor: app.globalData.system_color,
          success: res => {
            wx.switchTab({
              url: '../../order/index',
            })
          }
        })
      }
    })
  },
})